package xin.nick.manager;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import xin.nick.config.SystemProperties;
import xin.nick.entity.LoginUser;
import xin.nick.entity.SystemUser;
import xin.nick.mapper.SystemUserMapper;

import java.util.List;
import java.util.Objects;

/**
 * @author Nick
 * @since 2023/1/3
 */
@Component
@RequiredArgsConstructor
public class SystemUserManager {

    private final SystemUserMapper systemUserMapper;
    private final SystemProperties systemProperties;


    /**
     * 检查用户是否存在
     * @param userId
     */
    public void checkUser(Long userId) {
        Assert.notNull(userId, "[id]不可为空");
        SystemUser systemUser = systemUserMapper.selectById(userId);
        Assert.notNull(systemUser, "系统用户不存在");
    }

    /**
     * 根据条件查询list的第一个, 和 getOne 平替
     * @param queryWrapper
     * @return
     */
    public SystemUser getListFirst(Wrapper<SystemUser> queryWrapper) {
        List<SystemUser> systemUserList = systemUserMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(systemUserList)) {
            return null;
        }
        return systemUserList.get(0);
    }

    /**
     * 是否为rootId
     *
     * @param userId
     * @return
     */
    public boolean isRoot(Long userId) {
        return Objects.equals(systemProperties.getRootId(), userId);
    }

    /**
     * 检测是否有重复的账号
     * @param systemUser
     */
    public void checkAccount(SystemUser systemUser) {

        if (Objects.isNull(systemUser)) {
            return;
        }

        String username = systemUser.getUsername();
        boolean hasUsername = StringUtils.hasLength(username);
        Assert.isTrue(hasUsername, "username 为空");

        LambdaQueryWrapper<SystemUser> systemUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        systemUserLambdaQueryWrapper.eq(SystemUser::getUsername, username);
        List<SystemUser> userList = systemUserMapper.selectList(systemUserLambdaQueryWrapper);
        if (!CollectionUtils.isEmpty(userList)) {
            SystemUser databaseUser = userList.get(0);
            // 如果是更新用户的话,再判断一下 userId, 存在用户且不是当前修改的用户,则抛出错误
            boolean isSelf = Objects.equals(systemUser.getUserId(), databaseUser.getUserId());
            Assert.isTrue(isSelf, "已存在相同的 username");
        }

    }

    /**
     * 根据用户id获取登录用户信息
     * @param userId Long
     * @return LoginUser
     */
    public LoginUser getLoginUserByUserId(Long userId) {
        SystemUser systemUser = systemUserMapper.selectById(userId);
        if (Objects.isNull(systemUser)) {
            return null;
        }
        LoginUser loginUser = new LoginUser(systemUser.getNickname(), systemUser.getPassword());
        loginUser.setUserId(systemUser.getUserId());
        loginUser.setNickname(systemUser.getNickname());
        return loginUser;
    }
}
